/* Name: GammaRayBurst
 * Author: Cameron Hoerig
 * Date: 5-22-12
 * Description: The GammaRayBurst class is a random event that can occur. It is created only at the end 
 *  of a blackholes life if the blackhole is specified to contain a GammaRayBurst object. It destroys any 
 *  planets it comes into contact with and temporary disables colliding wormholes
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.LinkedList;

public class GammaRayBurst extends Item {
	private static LinkedList<GammaRayBurst> idList = new LinkedList<GammaRayBurst>();
	private Sprite sprite; // holds the sprite for the object
	private double rot; // the rotation of the line from the horizontal axis
	private Boolean growing = true;
	private int step = 1;
	private float rf;
	
	// Creates a gamma ray burst at the given location
	public GammaRayBurst(int px, int py) {
		Game.create((Item)this, 20); // Needed for update, draw, and destroy
		colType = COL_LINE; // Needed for collision checking
		idList.add(this); // Needed for collision checking
		rot = (double)Game.randomFloat((float) (Math.PI)); // randomly set the lines rotation
		// The object needs to extend to the edges of the screen, so the following lines calculate the endpoints
		double tempx = (double)px;
		double tempy = (double)py;
		double dx = 100*Math.cos(rot);
		double dy = 100*Math.sin(rot);
		// increment until the screen edge is hit
		while(tempx >= 0.0 & tempx <= (double)GameScreen.getWidth() & tempy >= 0 & tempy <= (double)GameScreen.getHeight()){
			tempx += dx;
			tempy += dy;
		}		
		x = (int)Math.floor(tempx);
		y = (int)Math.floor(tempy);
		tempx = (double)px;
		tempy = (double)py;
		// do same as above, but in opposite direction
		while(tempx >= 0.0 & tempx <= (double)GameScreen.getWidth() & tempy >= 0 & tempy <= (double)GameScreen.getHeight()){
			tempx -= dx;
			tempy -= dy;
		}		
		x2 = (int)Math.floor(tempx);
		y2 = (int)Math.floor(tempy);
		r = 1;
		rf = 1.0f;
		Wormhole.checkCollision(this, true);
		Planet.calculateDisFromEarth();
		// Play some sounds
		Sound.effect(Sound.EFFECT_ALARM);
		Sound.effect(Sound.EFFECT_GRB);
	}
	
	// Checks for collisions, and makes grow, etc.
	public void update() {
		Wormhole.checkCollision(this, true);
		Planet.checkCollision(this, true);
		
		// Draw
		if (growing) {
			step++;
			if (step >= 60*5) {growing = false;}
		} else {
			step--;
			if (step < 1) {step = 1;}
		}
		rf = (float) Math.exp(5*step/(float)(60*10));
		r = (int) rf;
	}
	
	// Handles collisions with the given object (deleting, etc)
	public void collision(Item other) {
		if(other instanceof Wormhole){ // disable the wormhole with which it is colliding
			Wormhole cOther = (Wormhole)other;
			cOther.active = false;
			cOther.setPlacementValidity(false);	
		}
	}
	
	public void destroy() {
		idList.remove(this); // Needed for collision checking
		Planet.calculateDisFromEarth();
	}
	
	// Called whenever a new frame is being drawn - draws the object
	public void draw() { // draw the object
		Graphics2D gh = Game.getGraphicsHandle();
		gh.setStroke(new BasicStroke(rf*2));
		gh.setColor(new Color(step/(float)(60*5*2), step/(float)(60*5), step/(float)(60*5*2), 0.85f));
		gh.drawLine(x, y, x2, y2);
		
	}
	
	// Function needed in each item class for collision checking
	public static int checkCollision(Item other, Boolean notify) {
		int num = 0;
		for (int i = 0; i < idList.size(); i++) {
			Item idOn = (Item)idList.get(i);
			if (idOn != other) {
				if (Item.intersection(other, idOn)) {
					if (notify) {
						idOn.collision(other);
					}
					num++;
				}
			}
		}
		return num;
	}
}